package cn.kevenpotter.leetcode.easy.e349;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Intersection {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2})));
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> resultSet = new HashSet<>();
        for (int value : nums1) set.add(value);
        for (int value : nums2) if (set.contains(value)) resultSet.add(value);
        Iterator<Integer> integerIterator = resultSet.iterator();
        int[] result = new int[resultSet.size()];
        int i = 0;
        while (integerIterator.hasNext()) result[i++] = integerIterator.next();
        return result;
    }
}
